﻿<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>我秀地图-添加dom标签</title>
    <meta charset="utf-8"/>
    <link href="" rel="shortcut icon" type="image/x-icon"/>
    <link href="../css/imap-gl.css" rel="stylesheet"/>
    <script src="../js/imap-gl.js"></script>
    <style>
        html, body {
            padding: 0;
            margin: 0;
            height: 100%;
            overflow: hidden;
        }

        #map {
            height: 100%;
            z-index: 0;
        }

    </style>
</head>
<body>
<div id="map"></div>
<script>
    var map = new imapgl.Map({
        container: 'map',
        center: [116.27771321722543, 39.952128183377255],
        zoom: 3,
        minZoom: 3,
        hash: true,
        maxZoom: 20,
        localIdeographFontFamily: "微软雅黑, 'Microsoft Sans Serif', sans-serif,宋体",
        style: "../styles/v1/light-v10-gcj02.json"
    });


    const geojson = {
        'type': 'FeatureCollection',
        'features': [
            {
                'type': 'Feature',
                'properties': {
                    'message': 'is yellow',
                    'iconColor': 'yellow'
                },
                'geometry': {
                    'type': 'Point',
                    'coordinates': [114.324462, 39.024695]
                }
            },
            {
                'type': 'Feature',
                'properties': {
                    'message': 'is blue',
                    'iconColor': 'blue'
                },
                'geometry': {
                    'type': 'Point',
                    'coordinates': [114.21582, 39.971891]
                }
            },
            {
                'type': 'Feature',
                'properties': {
                    'message': 'is red',
                    'iconColor': 'red'
                },
                'geometry': {
                    'type': 'Point',
                    'coordinates': [114.292236, 39.281518]
                }
            }
        ]
    };



    for (const marker of geojson.features) {

        const el = document.createElement('div');
        el.className = 'marker';
        el.style.backgroundColor = marker.properties.iconColor;
        el.style.width = `40px`;
        el.style.height = `40px`;
        el.style.backgroundSize = '100%';

        el.addEventListener('click', function () {
            window.alert(marker.properties.message);
        });

        new imapgl.Marker(el)
                .setLngLat(marker.geometry.coordinates)
                .addTo(map);
    }
</script>
</body>
</html>
